package Two.结构型模式.G1适配器模式;

/**
 * @author 李某
 * * @date 2021/11/5
 * *物有本末，事有终始。知其先后者，则近道矣！
 * *荷花开满池塘的前一天还是半塘荷花半塘云。
 */
interface TwoWayTarget
    //目标接口
    {
        public void request ();
    }

    //适配者接口
    interface TwoWayAdaptee {
        public void specificRequest();
    }

    //目标实现
    class TargetRealize implements TwoWayTarget {
        public void request() {
            System.out.println("目标代码被调用！");
        }
    }

    //适配者实现
    class AdapteeRealize implements TwoWayAdaptee {
        public void specificRequest() {
            System.out.println("适配者代码被调用！");
        }
    }

    //双向适配器
    class TwoWayAdapter implements TwoWayTarget, TwoWayAdaptee {
        private TwoWayTarget target;
        private TwoWayAdaptee adaptee;

        public TwoWayAdapter(TwoWayTarget target) {
            this.target = target;
        }

        public TwoWayAdapter(TwoWayAdaptee adaptee) {
            this.adaptee = adaptee;
        }

        public void request() {
            adaptee.specificRequest();
        }

        public void specificRequest() {
            target.request();
        }
    }

    //客户端代码
    class TwoWayAdapterTest {
        public static void main(String[] args) {
            System.out.println("目标通过双向适配器访问适配者：");
            TwoWayAdaptee adaptee = new AdapteeRealize();
            TwoWayTarget target = new TwoWayAdapter(adaptee);
            target.request();
            System.out.println("-------------------");
            System.out.println("适配者通过双向适配器访问目标：");
            target = new TargetRealize();
            adaptee = new TwoWayAdapter(target);
            adaptee.specificRequest();
        }
    }

